{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Visualizing and Analyzing Global Land Cover Data\n",
    "\n",
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/gee-community/geemap/blob/master/docs/workshops/GEE_Workshop_2022_Part2.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## Visualizing Global Land Cover Data Products"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "### Creating Dynamic World Land Cover Composites\n",
    "\n",
    "- App: https://www.dynamicworld.app\n",
    "- App2: https://earthoutreach.users.earthengine.app/view/dynamicworld\n",
    "- Paper: https://doi.org/10.1038/s41597-022-01307-4\n",
    "- Model: https://github.com/google/dynamicworld\n",
    "- Training data: https://doi.pangaea.de/10.1594/PANGAEA.933475\n",
    "- Data: https://developers.google.com/earth-engine/datasets/catalog/GOOGLE_DYNAMICWORLD_V1\n",
    "- JavaScript tutorial: https://developers.google.com/earth-engine/tutorials/community/introduction-to-dynamic-world-pt-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the region of interest by simply drawing a polygon on the map\n",
    "region = Map.user_roi\n",
    "if region is None:\n",
    "    region = ee.Geometry.BBox(-89.7088, 42.9006, -89.0647, 43.2167)\n",
    "    Map.addLayer(region, {}, \"Region\")\n",
    "\n",
    "Map.centerObject(region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the date range\n",
    "start_date = \"2021-01-01\"\n",
    "end_date = \"2022-01-01\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Sentinel-2 image composite\n",
    "image = geemap.dynamic_world_s2(region, start_date, end_date, clip=True)\n",
    "vis_params = {\"bands\": [\"B8\", \"B4\", \"B3\"], \"min\": 0, \"max\": 3000}\n",
    "Map.addLayer(image, vis_params, \"Sentinel-2 image\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create Dynamic World land cover composite\n",
    "landcover = geemap.dynamic_world(\n",
    "    region, start_date, end_date, clip=True, return_type=\"class\"\n",
    ")\n",
    "\n",
    "dwVisParams = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 8,\n",
    "    \"palette\": [\n",
    "        \"#419BDF\",\n",
    "        \"#397D49\",\n",
    "        \"#88B053\",\n",
    "        \"#7A87C6\",\n",
    "        \"#E49635\",\n",
    "        \"#DFC35A\",\n",
    "        \"#C4281B\",\n",
    "        \"#A59B8F\",\n",
    "        \"#B39FE1\",\n",
    "    ],\n",
    "}\n",
    "\n",
    "Map.addLayer(landcover, dwVisParams, \"Land Cover Class\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(title=\"Dynamic World Land Cover\", builtin_legend=\"Dynamic_World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = geemap.dynamic_world(\n",
    "    region, start_date, end_date, clip=True, return_type=\"visualize\"\n",
    ")\n",
    "Map.addLayer(landcover, {}, \"Land Cover Visualize\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = geemap.dynamic_world(\n",
    "    region, start_date, end_date, clip=True, return_type=\"probability\"\n",
    ")\n",
    "Map.addLayer(landcover, {}, \"Land Cover Probability\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create Dynamic World land cover composite\n",
    "landcover = geemap.dynamic_world(\n",
    "    region, start_date, end_date, clip=True, return_type=\"hillshade\"\n",
    ")\n",
    "Map.addLayer(landcover, {}, \"Land Cover\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/GEzsSii.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "### Comparing Global Land Cover Data Products"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "Visualizing [ESA Global Land Cover](https://developers.google.com/earth-engine/datasets/catalog/ESA_WorldCover_v200)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_date = \"2021-01-01\"\n",
    "end_date = \"2022-01-01\"\n",
    "region = ee.Geometry.BBox(-179, -89, 179, 89)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "esa = ee.ImageCollection(\"ESA/WorldCover/v200\").first()\n",
    "esa_vis = {\"bands\": [\"Map\"]}\n",
    "\n",
    "Map.addLayer(esa, esa_vis, \"ESA LC 2021\")\n",
    "Map.add_legend(title=\"ESA Land Cover\", builtin_legend=\"ESA_WorldCover\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21",
   "metadata": {},
   "source": [
    "Visualizing [ESRI Global Land Cover](https://gee-community-catalog.org/projects/S2TSLULC/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "esri = (\n",
    "    ee.ImageCollection(\n",
    "        \"projects/sat-io/open-datasets/landcover/ESRI_Global-LULC_10m_TS\"\n",
    "    )\n",
    "    .filterDate(start_date, end_date)\n",
    "    .mosaic()\n",
    ")\n",
    "esri_vis = {\n",
    "    \"min\": 1,\n",
    "    \"max\": 11,\n",
    "    \"palette\": [\n",
    "        \"#1A5BAB\",\n",
    "        \"#358221\",\n",
    "        \"#000000\",\n",
    "        \"#87D19E\",\n",
    "        \"#FFDB5C\",\n",
    "        \"#000000\",\n",
    "        \"#ED022A\",\n",
    "        \"#EDE9E4\",\n",
    "        \"#F2FAFF\",\n",
    "        \"#C8C8C8\",\n",
    "        \"#C6AD8D\",\n",
    "    ],\n",
    "}\n",
    "\n",
    "Map.addLayer(esri, esri_vis, \"ESRI LC 2021\")\n",
    "Map.add_legend(title=\"ESRI Land Cover\", builtin_legend=\"ESRI_LandCover_TS\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "dw_class = geemap.dynamic_world(region, start_date, end_date, return_type=\"class\")\n",
    "dw = geemap.dynamic_world(region, start_date, end_date, return_type=\"hillshade\")\n",
    "\n",
    "dw_vis = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 8,\n",
    "    \"palette\": [\n",
    "        \"#419BDF\",\n",
    "        \"#397D49\",\n",
    "        \"#88B053\",\n",
    "        \"#7A87C6\",\n",
    "        \"#E49635\",\n",
    "        \"#DFC35A\",\n",
    "        \"#C4281B\",\n",
    "        \"#A59B8F\",\n",
    "        \"#B39FE1\",\n",
    "    ],\n",
    "}\n",
    "\n",
    "Map.addLayer(dw_class, dw_vis, \"DW LC 2021\", False)\n",
    "Map.addLayer(dw, {}, \"DW LC Hillshade\")\n",
    "\n",
    "Map.add_legend(title=\"Dynamic World Land Cover\", builtin_legend=\"Dynamic_World\")\n",
    "Map.setCenter(-88.9088, 43.0006, 12)\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "Comparing Dynamic World and ESA Land Cover."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[39.3322, -106.7349], zoom=10)\n",
    "\n",
    "left_layer = geemap.ee_tile_layer(esa, esa_vis, \"ESA LC\")\n",
    "right_layer = geemap.ee_tile_layer(dw, {}, \"Dynamic World LC\")\n",
    "\n",
    "Map.split_map(left_layer, right_layer)\n",
    "Map.add_legend(\n",
    "    title=\"ESA Land Cover\", builtin_legend=\"ESA_WorldCover\", position=\"bottomleft\"\n",
    ")\n",
    "Map.add_legend(\n",
    "    title=\"Dynamic World Land Cover\",\n",
    "    builtin_legend=\"Dynamic_World\",\n",
    "    position=\"bottomright\",\n",
    ")\n",
    "Map.setCenter(-88.9088, 43.0006, 12)\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26",
   "metadata": {},
   "source": [
    "Comparing Dynamic World with ESRI Land Cover."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map(center=[-89.3998, 43.0886], zoom=10)\n",
    "\n",
    "left_layer = geemap.ee_tile_layer(esri, esri_vis, \"ESRI LC\")\n",
    "right_layer = geemap.ee_tile_layer(dw, {}, \"Dynamic World LC\")\n",
    "\n",
    "Map.split_map(left_layer, right_layer)\n",
    "Map.add_legend(\n",
    "    title=\"ESRI Land Cover\", builtin_legend=\"ESRI_LandCover_TS\", position=\"bottomleft\"\n",
    ")\n",
    "Map.add_legend(\n",
    "    title=\"Dynamic World Land Cover\",\n",
    "    builtin_legend=\"Dynamic_World\",\n",
    "    position=\"bottomright\",\n",
    ")\n",
    "Map.setCenter(-88.9088, 43.0006, 12)\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "### Creating Dynamic World Time Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the region of interest by simply drawing a polygon on the map\n",
    "region = Map.user_roi\n",
    "if region is None:\n",
    "    region = ee.Geometry.BBox(-89.7088, 42.9006, -89.0647, 43.2167)\n",
    "    Map.addLayer(region, {}, \"Region\")\n",
    "\n",
    "Map.centerObject(region)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the date range\n",
    "start_date = \"2017-01-01\"\n",
    "end_date = \"2023-01-01\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = geemap.dynamic_world_timeseries(\n",
    "    region, start_date, end_date, frequency=\"year\", return_type=\"class\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_params = {\n",
    "    \"min\": 0,\n",
    "    \"max\": 8,\n",
    "    \"palette\": [\n",
    "        \"#419BDF\",\n",
    "        \"#397D49\",\n",
    "        \"#88B053\",\n",
    "        \"#7A87C6\",\n",
    "        \"#E49635\",\n",
    "        \"#DFC35A\",\n",
    "        \"#C4281B\",\n",
    "        \"#A59B8F\",\n",
    "        \"#B39FE1\",\n",
    "    ],\n",
    "}\n",
    "Map.addLayer(images.first(), vis_params, \"First image\")\n",
    "Map.add_legend(title=\"Dynamic World Land Cover\", builtin_legend=\"Dynamic_World\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.ts_inspector(images, left_vis=vis_params, date_format=\"YYYY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "Map.centerObject(region)\n",
    "\n",
    "images = geemap.dynamic_world_timeseries(\n",
    "    region, start_date, end_date, frequency=\"year\", return_type=\"hillshade\"\n",
    ")\n",
    "Map.ts_inspector(images, date_format=\"YYYY\")\n",
    "Map.add_legend(title=\"Dynamic World Land Cover\", builtin_legend=\"Dynamic_World\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/5DGOuTC.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37",
   "metadata": {},
   "source": [
    "## Analyzing Global Land Cover Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "dataset = ee.ImageCollection(\"ESA/WorldCover/v200\").first()\n",
    "Map.addLayer(dataset, {\"bands\": [\"Map\"]}, \"ESA Land Cover\")\n",
    "Map.add_legend(title=\"ESA Land Cover Type\", builtin_legend=\"ESA_WorldCover\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.image_area_by_group(\n",
    "    dataset, scale=1000, denominator=1e6, decimal_places=4, verbose=True\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_csv(\"esa2021.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41",
   "metadata": {},
   "outputs": [],
   "source": [
    "esa_dict = {\n",
    "    \"10 Trees\": \"006400\",\n",
    "    \"20 Shrubland\": \"ffbb22\",\n",
    "    \"30 Grassland\": \"ffff4c\",\n",
    "    \"40 Cropland\": \"f096ff\",\n",
    "    \"50 Built-up\": \"fa0000\",\n",
    "    \"60 Barren / sparse vegetation\": \"b4b4b4\",\n",
    "    \"70 Snow and ice\": \"f0f0f0\",\n",
    "    \"80 Open water\": \"0064c8\",\n",
    "    \"90 Herbaceous wetland\": \"0096a0\",\n",
    "    \"95 Mangroves\": \"00cf75\",\n",
    "    \"100 Moss and lichen\": \"fae6a0\",\n",
    "}\n",
    "classes = list(esa_dict.keys())\n",
    "classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"class\"] = classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    df,\n",
    "    x=\"class\",\n",
    "    y=\"area\",\n",
    "    x_label=\"Land Cover Type\",\n",
    "    y_label=\"Area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(df, names=\"class\", values=\"area\", height=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45",
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ee.FeatureCollection(geemap.examples.get_ee_path(\"countries\"))\n",
    "Map.addLayer(countries, {}, \"Countries\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.zonal_stats_by_group(\n",
    "    dataset,\n",
    "    countries,\n",
    "    \"esa_2021_country.csv\",\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    scale=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"esa_2021_country.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"Class_10\",\n",
    "    max_rows=30,\n",
    "    x_label=\"Country\",\n",
    "    y_label=\"Forest Area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"esa_2021_country.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"Class_40\",\n",
    "    max_rows=30,\n",
    "    x_label=\"Country\",\n",
    "    y_label=\"Cropland Area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"esa_2021_country.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=[\"Class_10\", \"Class_20\", \"Class_30\", \"Class_40\"],\n",
    "    max_rows=10,\n",
    "    x_label=\"Country\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\n",
    "    \"esa_2021_country.csv\", names=\"NAME\", values=\"Class_10\", max_rows=30, height=500\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\n",
    "    \"esa_2021_country.csv\", names=\"NAME\", values=\"Class_40\", max_rows=30, height=500\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52",
   "metadata": {},
   "source": [
    "## Forest cover change analysis\n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2021_v1_9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image(\"UMD/hansen/global_forest_change_2021_v1_9\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.bandNames().getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56",
   "metadata": {},
   "outputs": [],
   "source": [
    "first_bands = [\"first_b50\", \"first_b40\", \"first_b30\"]\n",
    "first_image = dataset.select(first_bands)\n",
    "Map.addLayer(first_image, {\"bands\": first_bands, \"gamma\": 1.5}, \"Year 2000 Bands 5/4/3\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57",
   "metadata": {},
   "outputs": [],
   "source": [
    "last_bands = [\"last_b50\", \"last_b40\", \"last_b30\"]\n",
    "last_image = dataset.select(last_bands)\n",
    "Map.addLayer(last_image, {\"bands\": last_bands, \"gamma\": 1.5}, \"Year 2021 Bands 5/4/3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58",
   "metadata": {},
   "outputs": [],
   "source": [
    "treecover = dataset.select([\"treecover2000\"])\n",
    "\n",
    "treeCoverVisParam = {\"min\": 0, \"max\": 100, \"palette\": [\"black\", \"green\"]}\n",
    "\n",
    "name1 = \"Tree cover (%)\"\n",
    "Map.addLayer(treecover, treeCoverVisParam, name1)\n",
    "Map.add_colorbar(treeCoverVisParam, label=name1, layer_name=name1)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59",
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 10\n",
    "treecover_bin = treecover.gte(threshold).selfMask()\n",
    "treeVisParam = {\"palette\": [\"green\"]}\n",
    "Map.addLayer(treecover_bin, treeVisParam, \"Tree cover bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60",
   "metadata": {},
   "outputs": [],
   "source": [
    "treeloss_year = dataset.select([\"lossyear\"])\n",
    "\n",
    "treeLossVisParam = {\"min\": 0, \"max\": 21, \"palette\": [\"yellow\", \"red\"]}\n",
    "\n",
    "layer_name = \"Tree loss year\"\n",
    "Map.addLayer(treeloss_year, treeLossVisParam, layer_name)\n",
    "Map.add_colorbar(treeLossVisParam, label=layer_name, layer_name=layer_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61",
   "metadata": {},
   "outputs": [],
   "source": [
    "treeloss = dataset.select([\"loss\"]).selfMask()\n",
    "Map.addLayer(treeloss, {\"palette\": \"red\"}, \"Tree loss\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62",
   "metadata": {},
   "outputs": [],
   "source": [
    "treegain = dataset.select([\"gain\"]).selfMask()\n",
    "Map.addLayer(treegain, {\"palette\": \"yellow\"}, \"Tree gain\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63",
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ee.FeatureCollection(geemap.examples.get_ee_path(\"countries\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(countries)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65",
   "metadata": {},
   "outputs": [],
   "source": [
    "style = {\"color\": \"#ffff0088\", \"fillColor\": \"#00000000\"}\n",
    "Map.addLayer(countries.style(**style), {}, \"Countries\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66",
   "metadata": {},
   "source": [
    "Forest area analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.zonal_stats_by_group(\n",
    "    treecover_bin,\n",
    "    countries,\n",
    "    \"forest_cover.csv\",\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    scale=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\n",
    "    \"forest_cover.csv\", names=\"NAME\", values=\"Class_sum\", max_rows=20, height=600\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"forest_cover.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"Class_sum\",\n",
    "    max_rows=20,\n",
    "    x_label=\"Country\",\n",
    "    y_label=\"Forest area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70",
   "metadata": {},
   "source": [
    "Forest loss analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.zonal_stats_by_group(\n",
    "    treeloss,\n",
    "    countries,\n",
    "    \"treeloss.csv\",\n",
    "    stat_type=\"SUM\",\n",
    "    denominator=1e6,\n",
    "    scale=1000,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.pie_chart(\n",
    "    \"treeloss.csv\", names=\"NAME\", values=\"Class_sum\", max_rows=20, height=600\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.bar_chart(\n",
    "    \"treeloss.csv\",\n",
    "    x=\"NAME\",\n",
    "    y=\"Class_sum\",\n",
    "    max_rows=20,\n",
    "    x_label=\"Country\",\n",
    "    y_label=\"Forest loss area (km2)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74",
   "metadata": {},
   "source": [
    "## Surface water change analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75",
   "metadata": {},
   "source": [
    "### Surface water occurrence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image(\"JRC/GSW1_3/GlobalSurfaceWater\")\n",
    "dataset.bandNames().getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map.add_basemap(\"HYBRID\")\n",
    "\n",
    "image = dataset.select([\"occurrence\"])\n",
    "region = ee.Geometry.BBox(-99.957, 46.8947, -99.278, 47.1531)\n",
    "\n",
    "vis_params = {\"min\": 0.0, \"max\": 100.0, \"palette\": [\"ffffff\", \"ffbbbb\", \"0000ff\"]}\n",
    "\n",
    "Map.addLayer(image, vis_params, \"Occurrence\")\n",
    "Map.addLayer(region, {}, \"ROI\", True, 0.5)\n",
    "Map.centerObject(region)\n",
    "Map.add_colorbar(vis_params, label=\"Water occurrence (%)\", layer_name=\"Occurrence\")\n",
    "\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78",
   "metadata": {},
   "outputs": [],
   "source": [
    "hist = geemap.image_histogram(\n",
    "    image,\n",
    "    region,\n",
    "    scale=30,\n",
    "    x_label=\"Frequency\",\n",
    "    y_label=\"Pixel Count\",\n",
    "    title=\"Water Occurrence\",\n",
    "    return_df=False,\n",
    ")\n",
    "hist"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79",
   "metadata": {},
   "source": [
    "### Surace water monthly history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.ImageCollection(\"JRC/GSW1_3/MonthlyHistory\")\n",
    "size = dataset.size()\n",
    "print(size.getInfo())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dataset.aggregate_array(\"system:index\").getInfo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "image = dataset.filterDate(\"2020-08-01\", \"2020-09-01\").first()\n",
    "region = ee.Geometry.BBox(-99.957, 46.8947, -99.278, 47.1531)\n",
    "\n",
    "vis_params = {\"min\": 0.0, \"max\": 2.0, \"palette\": [\"ffffff\", \"fffcb8\", \"0905ff\"]}\n",
    "\n",
    "Map.addLayer(image, vis_params, \"Water\")\n",
    "Map.addLayer(region, {}, \"ROI\", True, 0.5)\n",
    "Map.centerObject(region)\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.jrc_hist_monthly_history(\n",
    "    region=region, scale=30, frequency=\"month\", denominator=1e4, return_df=True\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region, scale=30, frequency=\"month\", denominator=1e4, y_label=\"Area (ha)\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"month\",\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"month\",\n",
    "    y_label=\"Area (ha)\",\n",
    "    color=\"month\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"year\",\n",
    "    reducer=\"mean\",\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.jrc_hist_monthly_history(\n",
    "    region=region,\n",
    "    start_month=6,\n",
    "    end_month=9,\n",
    "    scale=30,\n",
    "    frequency=\"year\",\n",
    "    reducer=\"max\",\n",
    "    y_label=\"Area (ha)\",\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
